﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using Core.Model;
using System.Data.Objects;
using Core.UnitOfWork;
using Core.Repository;

namespace Core.Service
{
    public class Service<TEntity, TContext> : IService<TEntity>
        where TEntity : class,IKey<int>
        where TContext : ObjectContext,new()
    {

        #region IService 成员

        public virtual TEntity Get(object id)
        {
            using (TContext context = new TContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork.UnitOfWork(context);
                unitOfWork.LazyLoadingEnabled = false;
                unitOfWork.ProxyCreationEnabled = false;
                IRepository<TEntity> repository = new Repository<TEntity>(unitOfWork);
                return repository.Get(id);
            }
        }

        public virtual void Add(TEntity item)
        {
            using (TContext context = new TContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork.UnitOfWork(context);
                IRepository<TEntity> repository = new Repository<TEntity>(unitOfWork);
                repository.Add(item);
                unitOfWork.Save();
            }
        }

        public virtual void Update(TEntity item)
        {
            using (TContext context = new TContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork.UnitOfWork(context);
                IRepository<TEntity> repository = new Repository<TEntity>(unitOfWork);
                repository.Update(item);
                unitOfWork.Save();
            }
        }

        public virtual void Delete(TEntity item)
        {
            using (TContext context = new TContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork.UnitOfWork(context);
                IRepository<TEntity> repository = new Repository<TEntity>(unitOfWork);
                repository.Delete(item);
                unitOfWork.Save();
            }
        }

        public IList<TEntity> Find(Func<TEntity, bool> expression)
        {
            using (TContext context = new TContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork.UnitOfWork(context);
                unitOfWork.LazyLoadingEnabled = false;
                unitOfWork.ProxyCreationEnabled = false;
                IRepository<TEntity> repository = new Repository<TEntity>(unitOfWork);
                return repository.Find(expression);
            }
        }

        public virtual IList<TEntity> Find<Tkey>(Func<TEntity, bool> expression,
            Func<TEntity, Tkey>[] orderby, bool[] isDescending, int pageIndex, int pageCount)
        {
            using (TContext context = new TContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork.UnitOfWork(context);
                unitOfWork.LazyLoadingEnabled = false;
                unitOfWork.ProxyCreationEnabled = false;
                IRepository<TEntity> repository = new Repository<TEntity>(unitOfWork);
                return repository.Find(expression, orderby, isDescending, pageIndex, pageCount);
            }
        }

        #endregion
    }
}
